<?php

declare(strict_types=1);

namespace app\controller\admin\ec;

use think\Request;
use think\facade\Validate;
use app\eslib\EsUtils;
use app\model\ec\OrderModel;

/**
 * 订单 控制器
 * @version 1.0.0
 */
class Orders
{
    // 控制器中间件
    protected $middleware = [\app\middleware\JwtAuthForAdmin::class];

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $searchText = $request->param('search_text', '');
        $pageSize = intval($request->param('page_size', 10));

        $data = OrdersModel::where(function ($query) use ($searchText) {
            if ($searchText) {
                $query->where(function ($query) use ($searchText) {
                    $query->where('id', intval($searchText))->whereOr('user_name', 'like', "%{$searchText}%");
                });
            }
        })->order('status')->order('id', 'desc')->paginate($pageSize);
        if ($data) {
            $data->append(['flag_text', 'payment_type_text', 'is_visible_text', 'status_text', 'promoter_commission_status_text', 'evaluation_status_text', 'pay_status_text', 'source_text', 'refund_type_text', 'return_type_text', 'return_status_text', 'return_audit_status_text', 'return_delivery_status_text']);
        }
        return json(EsUtils::stdMessage(true, '', $data));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $formData = $request->param();
        $validator =  Validate::rule([
            'user_id|买家ID' => 'require|number',
'mch_id|商户ID' => 'require|number',
'order_no|订单编号' => 'require|max:32',
'quantity|商品数量' => 'require|number',
'total_money|订单总价' => 'require|number',
'postage_money|运费' => 'require|number',
'pay_money|实际支付金额' => 'require|number',
'pay_postage_money|实际支付运费' => 'require|number',
'deduction_money|抵扣金额' => 'require|number',
'give_integral|订单赠送积分' => 'require|number',
'gain_integral|消费赚取积分' => 'require|number',
'use_integral|使用积分' => 'require|number',
'coupon_id|优惠券ID' => 'require|number',
'coupon_money|优惠券金额' => 'require|number',
'flag|订单标识' => 'require|max:16',
'extra|扩展数据' => 'require|max:200',
'payment_type|支付方式' => 'require|max:16',
'trade_no|第三方交易单号' => 'require|max:32',
'is_visible|是否显示' => 'require|number',
'status|订单状态' => 'require|number',
'promoter_commission_status|分销提成状态' => 'require|number',
'evaluation_status|评价状态' => 'require|number',
'pay_status|支付状态' => 'require|number',
'delivery_name|收货人姓名' => 'require|max:60',
'delivery_phone|收货人电话' => 'require|max:18',
'delivery_address|收货人详细地址' => 'require|max:100',
'express_id|快递公司ID' => 'require|number',
'express_no|快递单号' => 'require|max:32',
'buyer_remark|买家备注' => 'require|max:512',
'admin_remark|管理员备注' => 'require|max:512',
'confirm_remark|订单确认备注' => 'require|max:512',
'source|订单来源' => 'require|number',
'return_no|退货编号' => 'require|max:32',
'refund_type|申请类型' => 'require|number',
'return_type|退货类型' => 'require|number',
'return_status|退货状态' => 'require|number',
'return_quantity|退货商品数量' => 'require|number',
'refund_money|退款金额' => 'require|number',
'return_buyer_message|退款退货申请原因' => 'require|max:255',
'return_buyer_images|退款退货凭证图片' => 'require|max:255',
'return_audit_status|管理员审核状态' => 'require|number',
'return_audit_message|管理员审核结果' => 'require|max:255',
'return_delivery_status|物流状态' => 'require|number',
'return_express_id|物流公司编号' => 'require|number',
'return_express_no|物流单号' => 'require|max:32',
'return_receive_message|收货备注' => 'require|max:300',
        ]);
        if (!$validator->check($formData)) {
            return json(EsUtils::stdMessage(false, $validator->getError()));
        }
        // 表单数据处理
        // ...
        try {
            $model = OrdersModel::create($formData);
            return json(EsUtils::stdMessage(true, '', $model->id));
        } catch (\Exception $ex) {
            return json(EsUtils::stdMessage(false, $ex->getMessage()));
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        if (!is_numeric($id)) {
            return json(EsUtils::stdMessage(false, 'id值无效', $id));
        }
        if ($id > 0) {
            $model = OrdersModel::find($id);
            if (!$model) {
                return json(EsUtils::stdMessage(false, '记录不存在'));
            }
        } else {
            $model = new OrdersModel();
            $model->flag = '';
$model->payment_type = '';
$model->is_visible = 1;
$model->status = 0;
$model->promoter_commission_status = 0;
$model->evaluation_status = 0;
$model->pay_status = 0;
$model->source = 0;
$model->refund_type = 0;
$model->return_type = 0;
$model->return_status = 0;
$model->return_audit_status = 0;
$model->return_delivery_status = 0;
$model->mch_id = 0;
$model->quantity = 0;
$model->total_money = 0.00;
$model->postage_money = 0.00;
$model->pay_money = 0.00;
$model->pay_postage_money = 0.00;
$model->deduction_money = 0.00;
$model->give_integral = 0;
$model->gain_integral = 0.00;
$model->use_integral = 0.00;
$model->coupon_id = 0;
$model->coupon_money = 0.00;
$model->is_visible = 1;
$model->status = 0;
$model->promoter_commission_status = 0;
$model->evaluation_status = 0;
$model->pay_status = 0;
$model->express_id = 0;
$model->source = 0;
$model->refund_type = 0;
$model->return_type = 0;
$model->return_status = 0;
$model->return_quantity = 0;
$model->refund_money = 0.00;
$model->return_audit_status = 0;
$model->return_delivery_status = 0;
$model->return_express_id = 0;
        }
        $data = [
            'data' => $model,
            'flag_data' => $model->getFlagDataAttr(),
'payment_type_data' => $model->getPaymentTypeDataAttr(),
'is_visible_data' => $model->getIsVisibleDataAttr(),
'status_data' => $model->getStatusDataAttr(),
'promoter_commission_status_data' => $model->getPromoterCommissionStatusDataAttr(),
'evaluation_status_data' => $model->getEvaluationStatusDataAttr(),
'pay_status_data' => $model->getPayStatusDataAttr(),
'source_data' => $model->getSourceDataAttr(),
'refund_type_data' => $model->getRefundTypeDataAttr(),
'return_type_data' => $model->getReturnTypeDataAttr(),
'return_status_data' => $model->getReturnStatusDataAttr(),
'return_audit_status_data' => $model->getReturnAuditStatusDataAttr(),
'return_delivery_status_data' => $model->getReturnDeliveryStatusDataAttr(),
        ];
        return json(EsUtils::stdMessage(true, $id, $data));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        // 特殊模式的更新操作
        $model = $request->param('_mode', '');
        switch ($model) {
            case 'copy':
                // 复制指定的资源
                try {
                    $affected = OrdersModel::copy($id);
                    return json(EsUtils::stdMessage(true, '', $affected));
                } catch (\Exception $ex) {
                    return json(EsUtils::stdMessage(false, $ex->getMessage()));
                }
            case 'uoc':
                // uoc = update one column，更新某一字段
                $field = $request->param('k', '');
                $value = $request->param('v', '');
                try {
                    $affected = OrdersModel::where('id', $id)->update([$field => $value]);
                    return json(EsUtils::stdMessage(true, '', $affected));
                } catch (\Exception $ex) {
                    return json(EsUtils::stdMessage(false, $ex->getMessage()));
                }
                break;
            default:
                break;
        }

        // 常规更新操作
        $formData = $request->param();
        $validator =  \think\facade\Validate::rule([
            'user_id|买家ID' => 'require|number',
'mch_id|商户ID' => 'require|number',
'order_no|订单编号' => 'require|max:32',
'quantity|商品数量' => 'require|number',
'total_money|订单总价' => 'require|number',
'postage_money|运费' => 'require|number',
'pay_money|实际支付金额' => 'require|number',
'pay_postage_money|实际支付运费' => 'require|number',
'deduction_money|抵扣金额' => 'require|number',
'give_integral|订单赠送积分' => 'require|number',
'gain_integral|消费赚取积分' => 'require|number',
'use_integral|使用积分' => 'require|number',
'coupon_id|优惠券ID' => 'require|number',
'coupon_money|优惠券金额' => 'require|number',
'flag|订单标识' => 'require|max:16',
'extra|扩展数据' => 'require|max:200',
'payment_type|支付方式' => 'require|max:16',
'trade_no|第三方交易单号' => 'require|max:32',
'is_visible|是否显示' => 'require|number',
'status|订单状态' => 'require|number',
'promoter_commission_status|分销提成状态' => 'require|number',
'evaluation_status|评价状态' => 'require|number',
'pay_status|支付状态' => 'require|number',
'delivery_name|收货人姓名' => 'require|max:60',
'delivery_phone|收货人电话' => 'require|max:18',
'delivery_address|收货人详细地址' => 'require|max:100',
'express_id|快递公司ID' => 'require|number',
'express_no|快递单号' => 'require|max:32',
'buyer_remark|买家备注' => 'require|max:512',
'admin_remark|管理员备注' => 'require|max:512',
'confirm_remark|订单确认备注' => 'require|max:512',
'source|订单来源' => 'require|number',
'return_no|退货编号' => 'require|max:32',
'refund_type|申请类型' => 'require|number',
'return_type|退货类型' => 'require|number',
'return_status|退货状态' => 'require|number',
'return_quantity|退货商品数量' => 'require|number',
'refund_money|退款金额' => 'require|number',
'return_buyer_message|退款退货申请原因' => 'require|max:255',
'return_buyer_images|退款退货凭证图片' => 'require|max:255',
'return_audit_status|管理员审核状态' => 'require|number',
'return_audit_message|管理员审核结果' => 'require|max:255',
'return_delivery_status|物流状态' => 'require|number',
'return_express_id|物流公司编号' => 'require|number',
'return_express_no|物流单号' => 'require|max:32',
'return_receive_message|收货备注' => 'require|max:300',
        ]);
        if (!$validator->check($formData)) {
            return json(EsUtils::stdMessage(false, $validator->getError()));
        }
        try {
            $affected = OrdersModel::where('id', $id)->update($formData);
            return json(EsUtils::stdMessage(true, '', $affected));
        } catch (\Exception $ex) {
            return json(EsUtils::stdMessage(false, $ex->getMessage()));
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        try {
            $affected = OrdersModel::destroy($id);
            return json(EsUtils::stdMessage(true, '', $affected));
        } catch (\Exception $ex) {
            return json(EsUtils::stdMessage(false, $ex->getMessage()));
        }
    }
}
